package com.example.dessert.controller.fore;

import com.example.dessert.annotation.LoginCheck;
import com.example.dessert.controller.BaseController;
import com.example.dessert.entity.Orders;
import com.example.dessert.entity.Product;
import com.example.dessert.entity.ProductSpec;
import com.example.dessert.service.OrderService;
import com.example.dessert.util.ResponsePojo;
import com.example.dessert.util.ResultCode;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: PayController
 * @Description: 支付
 * @Author li
 * @Date 2022/1/19
 * @Version 1.0
 */
@Controller
@RequestMapping("/fore/pay")
public class PayController extends BaseController {

    @Resource
    private OrderService orderService;

    /**
     * 支付页面
     */
    @GetMapping("/order")
    @LoginCheck
    public String order(Model model, @RequestParam Map<String, Object> map){

        Long orderId = Long.parseLong(map.get("orderId").toString());

        Orders order = orderService.queryOrderById(orderId);

        model.addAttribute("order", order);

        return "fore/pay/order";

    }

    /**
     * 查询订单支付状态
     */
    @PostMapping("/queryOrderPayStatus")
    @LoginCheck
    @ResponseBody
    public ResponsePojo queryOrderPayStatus(HttpSession session, @RequestParam Map<String, Object> map) {

        logger.info("Buy items now Request Parameters:{}", map);

        Object userId = session.getAttribute("userId");
        Long orderId = Long.parseLong(map.get("orderId").toString());

        // 验证订单是否存在
        Orders orders = orderService.queryOrderById(orderId);
        if(orders == null){
            return ResponsePojo.error(ResultCode.EXP_INVALID_PRODUCT,"订单不存在");
        }

        //修改订单状态
        Orders o = new Orders();
        o.setId(orderId)
         .setPayStatus(1)
         .setPayAt(new Date());
        orderService.saveOrder(o);

        return ResponsePojo.ok();

    }


}
